var config = require('../../utils/config');
var util = require('../../utils/util');
var db = require('../../utils/db');
var app = getApp();
Page({
  data:{
    pageNo:1,
    pageSize:10,
    bookList:[],
    isLogin:false
  },
  onShow:function() {
    var that = this;
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!=''&&userInfo.userId!=undefined) {
      that.queryBook(userInfo, true);
    }else {
      //调用应用实例的方法获取全局数据
      app.getUserInfo(function(userInfo) {
        setTimeout(()=>{
          userInfo = wx.getStorageSync('user');
          if (userInfo.userId) {
            that.queryBook(userInfo, true);
          }else {
            that.queryBook(userInfo, false);
          }
        }, 2000);
      })
    }
  },
  onReachBottom(){
    setTimeout(()=>{
      this.getMoreData();
    },500)
  },
  getMoreData() {
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!=''&&userInfo.userId!=undefined) {
      var that = this;
      that.data.pageNo++;
      var options = {
        url: config.clubApi.getBook,
        data: {
          userId: userInfo.userId,
          pageNo: that.data.pageNo,
          pageSize: that.data.pageSize
        }
      };
      util.request(options, function(res) {
        if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
          var books = res.data.data.pageData;
          that.setData({
            bookList:books
          })
        }
      });
    }
  },
  queryBook:function(userInfo, isLogin) {
    var that = this;
    if (userInfo.userId!=undefined) {
      var options = {
        url: config.clubApi.getBook,
        data: {
          userId: userInfo.userId,
          pageNo: that.data.pageNo,
          pageSize: that.data.pageSize
        }
      };
      util.request(options, function(res) {
        if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
          var books = res.data.data.pageData;
          that.setData({
            bookList:books,
            isLogin:isLogin
          })
        }
      });
    }
  },
  addBook:function() {
    wx.navigateTo({
      url: "../bookList/bookList"
    });
  },
  goToContentPage:function(e) {
    var ary = e.currentTarget.id.split('^');
    wx.navigateTo({
      url: '../bookContent/bookContent?id='+ary[0]+'&userbookId='+ary[1]+'&jxkey='+ary[2]
    });
  },
  getPhoneNumber (e) {
    if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){//拒绝获取手机号
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) {}
      })
    }else if(e.detail.errMsg.indexOf('getPhoneNumber:fail Error')>=0){//不支持获取手机号
    }else{//同意获取手机号
      var that = this;
      wx.login({
        success: function (resLogin) {
          let code = resLogin.code;
          wx.getUserInfo({
            success: function (res) {
              that.setData({
                isLogin:true
              })
              db.putOpenid(code, (resOpen, err) => {
                if (resOpen.data.data.userId) {
                  app.setUserInfo(resOpen.data.data);
                  setTimeout(()=>{
                    that.data.userInfo = wx.getStorageSync('user');
                    that.onShow();
                  }, 2000);
                }else {
                  let openId = resOpen.data.data.openid;
                  var option = {
                    method: 'GET',
                    url: config.clubApi.getPhone,
                    data: {
                      encryptedData: e.detail.encryptedData,
                      iv: e.detail.iv,
                      sessionKey: resOpen.data.data.sessionKey,
                      openid: openId
                    }
                  };
                  util.request(option, (res) => {
                    if (res.statusCode===200) {
                      app.queryUser(openId);
                      setTimeout(()=>{
                        that.data.userInfo = wx.getStorageSync('user');
                        that.onShow();
                      }, 2000);
                    }else {
                      that.setData({
                        isLogin:false
                      })
                    }
                  });
                }
              })
            }
          })
        }
      })
    }
  }
})